package com.woniuxy.secondhand.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.woniuxy.life.core.api.CommonResult;
import com.woniuxy.secondhand.entity.SecGoods;
import com.woniuxy.secondhand.mapper.SecGoodsMapper;
import com.woniuxy.secondhand.service.SecGoodsService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 发布的产品 前端控制器
 * </p>
 *
 * @author chaohuijiang
 * @since 2020-09-24
 */
@RestController
@RequestMapping("/secondhand/sec-goods")
public class SecGoodsController {
    @Resource
    SecGoodsService secGoodsService;
    @Resource
    SecGoodsMapper secGoodsMapper;
//商品发布
    @PostMapping("/insert")
    public CommonResult<SecGoods> publishGoods(@RequestBody SecGoods secGoods){
        //存入数据库
        System.out.println(secGoods);
       boolean re= secGoodsService.save(secGoods);
       if (re){
            return  CommonResult.success();
        }
        return CommonResult.success();

    }
    //查询商品
    //删除发布商品
    @DeleteMapping("/delete/{secGoodsId}")
    public CommonResult<SecGoods> delete(@PathVariable("secGoodsId") Integer secGoodsId){
        boolean re=secGoodsService.removeById(secGoodsId);
        if (re){
            return  CommonResult.success();
        }
        return CommonResult.failed();
    }
    //修改发布信息
    @PutMapping("/update")
    public CommonResult<SecGoods> update(SecGoods secGoods){
        UpdateWrapper<SecGoods> updateWrapper=new UpdateWrapper<>();
        boolean re=secGoodsService.update(updateWrapper);
        if (re){
            return  CommonResult.success();
        }
        return CommonResult.failed();
    }
    //id查询商品详细信息
    @RequestMapping("/select/{secGoodsId}")
    public ModelAndView selectOne(@PathVariable("secGoodsId") Integer secGoodsId){
        ModelAndView mv=new ModelAndView();
        SecGoods secGoods=secGoodsService.getById(secGoodsId);
        mv.addObject("good",secGoods);
        mv.setViewName("goodsInfo");
        return mv;
    }
    //查询所有已经发布产品
    @RequestMapping("/findall")
    @ResponseBody
    public ModelAndView selectAll(){
        ModelAndView mv=new ModelAndView();
        QueryWrapper<SecGoods> queryWrapper=new QueryWrapper<>();
        List<SecGoods> goods= secGoodsMapper.selectList(queryWrapper);
        System.out.println(goods);
        mv.addObject("goods",goods);
        mv.setViewName("index");
        return mv;
    }
}
